class Solution {
    //买卖股票的最佳时机III
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int k = 3;
        //创建dp表
        int[][] f = new int[n][k];//买入
        int[][] g = new int[n][k];//卖出
        //初始化
        int INF = 0x3f3f3f3f;
        for (int i = 0; i < k; i++) {
            f[0][i] = g[0][i] = -INF;
        }
        f[0][0] = -prices[0];
        g[0][0] = 0;
        //填表
        for(int i = 1; i < n; i++){
            for(int j = 0; j < k; j++){
                f[i][j] = Math.max(f[i-1][j], g[i-1][j] - prices[i]);
                g[i][j] = g[i-1][j];
                if(j > 0) g[i][j] = Math.max(g[i][j], f[i-1][j-1] + prices[i]);
            }
        }
        //返回结果
        int max = 0;
        for(int i = 0; i < k; i++){
            if(g[n-1][i] > max) max = g[n-1][i];
        }
        return max;
    }
    //买卖股票的最佳时机IV
    public int maxProfit(int k, int[] prices) {
        int n = prices.length;
        //处理细节问题
        k = Math.min(k, n/2);
        //创建dp表
        int[][] f = new int[n][k+1];//买入
        int[][] g = new int[n][k+1];//卖出
        //初始化
        int INF = 0x3f3f3f3f;
        for (int i = 0; i <= k; i++) {
            f[0][i] = g[0][i] = -INF;
        }
        f[0][0] = -prices[0];
        g[0][0] = 0;
        //填表
        for(int i = 1; i < n; i++){
            for(int j = 0; j <= k; j++){
                f[i][j] = Math.max(f[i-1][j], g[i-1][j] - prices[i]);
                g[i][j] = g[i-1][j];
                if(j > 0) g[i][j] = Math.max(g[i][j], f[i-1][j-1] + prices[i]);
            }
        }
        //返回结果
        int max = 0;
        for(int i = 0; i <= k; i++){
            if(g[n-1][i] > max) max = g[n-1][i];
        }
        return max;
    }
    //最大子数组和
    public int maxSubArray(int[] nums) {
        int n = nums.length;
        //创建dp表
        int[] dp = new int[n+1];
        //填表
        int max = nums[0];
        for(int i = 1; i <= n; i++){
            dp[i] = Math.max(nums[i-1], dp[i-1] + nums[i-1]);
            max = Math.max(max, dp[i]);
        }
        //返回
        return max;
    }
    //环形子数组的最大和
    public int maxSubarraySumCircular(int[] nums) {
        int n = nums.length;
        //创建dp表
        int[] f = new int[n+1];//最大和
        int[] g = new int[n+1];//最小和
        int max = nums[0], min = nums[0];
        //初始化

        //填表
        int sum = 0;
        for(int i = 1; i <= n; i++){
            sum += nums[i-1];
            f[i] = Math.max(f[i-1] + nums[i-1], nums[i-1]);
            max = Math.max(f[i], max);
            g[i] = Math.min(g[i-1] + nums[i-1], nums[i-1]);
            min = Math.min(g[i], min);
        }
        if(min == sum) return max;
        else return Math.max(max, sum - min);
    }
    //乘积最大子数组
    public int maxProduct(int[] nums) {
        int n = nums.length;
        //创建dp表
        int[] f = new int[n+1];//最大乘积 +
        int[] g = new int[n+1];//最小乘积 -
        int max = nums[0];
        //初始化
        f[0] = -1111;
        g[0] = 1111;
        //填表
        for(int i = 1; i <= n; i++){
            if(nums[i-1] >= 0){
                f[i] = Math.max(nums[i-1], f[i-1] * nums[i-1]);
                g[i] = g[i-1]*nums[i-1];
            }else{
                g[i] = Math.min(nums[i-1], f[i-1] * nums[i-1]);
                f[i] = g[i-1] * nums[i-1];
            }
            max = Math.max(max, f[i]);
        }
        return max;
    }
    //乘积为正数的最长子数组长度
    public int getMaxLen(int[] nums) {
        int n = nums.length;
        //创建dp表
        int[] f = new int[n+1];
        int[] g = new int[n+1];
        //初始化
        int max = 0;
        //填表
        for(int i = 1; i <= n; i++){
            if(nums[i-1] > 0){
                f[i] = f[i-1] + 1;
                if(g[i-1] > 0)g[i] = g[i-1] + 1;
            }else if(nums[i-1] < 0){
                if(g[i-1] > 0) f[i] = g[i-1] + 1;
                g[i] = f[i-1] + 1;
            }else{
                f[i] = g[i] = 0;
            }
            max = Math.max(f[i], max);
        }
        return max;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] prices = {-1, -2, -3, 0, 1};
        solution.maxProfit(prices);
        solution.getMaxLen(prices);
        solution.maxSubarraySumCircular(prices);
    }
}